! *************************************************************
 subroutine calfun(nparam_dum,param_dum,val)
 use mainmodule
 use tauchen_mod
 implicit none
 integer,intent(in)                 :: nparam_dum
 real(prec),dimension(:),intent(in) :: param_dum(nparam_dum)
 real(prec),intent(out)             :: val

 !integer :: mv
 real(prec),dimension(:), allocatable :: v_err

 allocate(v_err(mv))

if(calibrcase.eq.1) then
     !phi1=(phi1_h-phi1_l)*param_dum(1)+phi1_l
     Psimin=(Psimin_h-Psimin_l)*param_dum(1)+Psimin_l
     beta=(beta_h-beta_l)*param_dum(2)+beta_l !18jan15

    open(299, file='adjusted_param.dat')
    write(299,*) Psimin, beta
    !write(299,*) phi1,  gamma_psi, Psimin, beta, Rbar(1), sdeta
    close(299)
    open(299, file='adjusted_param.dat',status='old')
    read(299,*) Psimin, beta
    close(299)

        call initstuff
        call stat_distr
        call prices_iter
        call simul_paths

        v_err(1)=   (AtoY-AtoY_dat)/AtoY_dat
        v_err(2)=   (spr_stdv-sprstdv_dat)/sprstdv_dat
!        v_err(4)=   (debt_stdv-debtstdv_dat)/debtstdv_dat

!        v_err(1)=   3.0_prec*v_err(1)
!        v_err(2)=   2.0_prec*v_err(2)

end if

if(calibrcase.eq.2) then
     Psimin=(Psimin_h-Psimin_l)*param_dum(1)+Psimin_l
     beta=(beta_h-beta_l)*param_dum(2)+beta_l !18jan15
     phi1=(phi1_h-phi1_l)*param_dum(3)+phi1_l
     pinu1=(pinu1_h-pinu1_l)*param_dum(4)+pinu1_l

    open(299, file='adjusted_param.dat')
    write(299,*) Psimin, beta, phi1, pinu1
    !write(299,*) phi1,  gamma_psi, Psimin, beta, Rbar(1), sdeta
    close(299)
    open(299, file='adjusted_param.dat',status='old')
    read(299,*) Psimin, beta, phi1, pinu1
    close(299)

     pinu(1,1)=pinu1
     pinu(2,1)=pinu1
     pinu(1,2)=one-pinu1
     pinu(2,2)=one-pinu1

        call initstuff
        call stat_distr
        call prices_iter
        call simul_paths

        v_err(1)=   (AtoY-AtoY_dat)/AtoY_dat
        v_err(2)=   (spr_stdv-sprstdv_dat)/sprstdv_dat
        v_err(3)=   (debt_stdv-debtstdv_dat)/debtstdv_dat
        v_err(4)=   (d_stdv-dstdv_dat)/dstdv_dat
        v_err(5)=   (freqdef-freqdef_dat)/freqdef_dat
        v_err(6)=   (arrears-arrears_dat)/arrears_dat

!        v_err(1)=   3.0_prec*v_err(1)
!        v_err(2)=   2.0_prec*v_err(2)

end if

if(calibrcase.eq.3) then
     Psimin=(Psimin_h-Psimin_l)*param_dum(1)+Psimin_l
     beta=(beta_h-beta_l)*param_dum(2)+beta_l !18jan15
     pinu1=(pinu1_h-pinu1_l)*param_dum(3)+pinu1_l

    open(299, file='adjusted_param.dat')
    write(299,*) Psimin, beta, pinu1
    !write(299,*) phi1,  gamma_psi, Psimin, beta, Rbar(1), sdeta
    close(299)
    open(299, file='adjusted_param.dat',status='old')
    read(299,*) Psimin, beta, pinu1
    close(299)

     pinu(1,1)=pinu1
     pinu(2,1)=pinu1
     pinu(1,2)=one-pinu1
     pinu(2,2)=one-pinu1

        call initstuff
        call stat_distr
        call prices_iter
        call simul_paths

        v_err(1)=   (AtoY-AtoY_dat)/AtoY_dat
        v_err(2)=   (spr_stdv-sprstdv_dat)/sprstdv_dat
        v_err(3)=   (hhaircut-hhaircut_dat)/hhaircut_dat

!        v_err(1)=   3.0_prec*v_err(1)
!        v_err(2)=   2.0_prec*v_err(2)

end if

if(calibrcase.eq.4) then
     Psimin=(Psimin_h-Psimin_l)*param_dum(1)+Psimin_l
     beta=(beta_h-beta_l)*param_dum(2)+beta_l !18jan15
     pinu1=(pinu1_h-pinu1_l)*param_dum(3)+pinu1_l

    open(299, file='adjusted_param.dat')
    write(299,*) Psimin, beta, pinu1
    !write(299,*) phi1,  gamma_psi, Psimin, beta, Rbar(1), sdeta
    close(299)
    open(299, file='adjusted_param.dat',status='old')
    read(299,*) Psimin, beta, pinu1
    close(299)

     pinu(1,1)=pinu1
     pinu(2,1)=pinu1
     pinu(1,2)=one-pinu1
     pinu(2,2)=one-pinu1

        call initstuff
        call stat_distr
        call prices_iter
        call simul_paths

        v_err(1)=   (AtoY-AtoY_dat)/AtoY_dat
        v_err(2)=   (spr_stdv-sprstdv_dat)/sprstdv_dat
        v_err(3)=   (debt_stdv-debtstdv_dat)/debtstdv_dat

!        v_err(1)=   3.0_prec*v_err(1)
!        v_err(2)=   2.0_prec*v_err(2)

end if

if(calibrcase.eq.5) then
     Psimin=(Psimin_h-Psimin_l)*param_dum(1)+Psimin_l
     beta=(beta_h-beta_l)*param_dum(2)+beta_l !18jan15
     pinu1=(pinu1_h-pinu1_l)*param_dum(3)+pinu1_l
     phi1=(phi1_h-phi1_l)*param_dum(4)+phi1_l


    open(299, file='adjusted_param.dat')
    write(299,*) Psimin, beta, pinu1, phi1
    !write(299,*) phi1,  gamma_psi, Psimin, beta, Rbar(1), sdeta
    close(299)
    open(299, file='adjusted_param.dat',status='old')
    read(299,*) Psimin, beta, pinu1, phi1
    close(299)

     pinu(1,1)=pinu1
     pinu(2,1)=pinu1
     pinu(1,2)=one-pinu1
     pinu(2,2)=one-pinu1

        call initstuff
        call stat_distr
        call prices_iter
        call simul_paths

        v_err(1)=   (AtoY-AtoY_dat)/AtoY_dat
        v_err(2)=   (spr_stdv-sprstdv_dat)/sprstdv_dat
        v_err(3)=   (debt_stdv-debtstdv_dat)/debtstdv_dat
        v_err(4)=   (hhaircut-hhaircut_dat)/hhaircut_dat

!        v_err(1)=   3.0_prec*v_err(1)
!        v_err(2)=   2.0_prec*v_err(2)

end if

if(calibrcase.eq.6) then
     Psimin=(Psimin_h-Psimin_l)*param_dum(1)+Psimin_l
     beta=(beta_h-beta_l)*param_dum(2)+beta_l !18jan15
     phi1=(phi1_h-phi1_l)*param_dum(3)+phi1_l
     pinu1=(pinu1_h-pinu1_l)*param_dum(4)+pinu1_l

    open(299, file='adjusted_param.dat')
    write(299,*) Psimin, beta, pinu1, phi1
    !write(299,*) phi1,  gamma_psi, Psimin, beta, Rbar(1), sdeta
    close(299)
    open(299, file='adjusted_param.dat',status='old')
    read(299,*) Psimin, beta, pinu1, phi1
    close(299)

     pinu(1,1)=pinu1
     pinu(2,1)=pinu1
     pinu(1,2)=one-pinu1
     pinu(2,2)=one-pinu1

        call initstuff
        call stat_distr
        call prices_iter
        call simul_paths

        v_err(1)=   (AtoY-AtoY_dat)/AtoY_dat
        v_err(2)=   (spr_stdv-sprstdv_dat)/sprstdv_dat
        v_err(3)=   (debt_stdv-debtstdv_dat)/debtstdv_dat
        v_err(4)=   (d_stdv-dstdv_dat)/dstdv_dat
        v_err(5)=   (freqdef-freqdef_dat)/freqdef_dat
        v_err(6)=   (arrears-arrears_dat)/arrears_dat
        v_err(7)=   (debtserv-debtserv_dat)/debtserv_dat
        v_err(8)=   (debtserv_sd-debtserv_sd_dat)/debtserv_sd_dat

!        v_err(1)=   3.0_prec*v_err(1)
!        v_err(2)=   2.0_prec*v_err(2)

end if

if(calibrcase.eq.7) then
     Psimin=(Psimin_h-Psimin_l)*param_dum(1)+Psimin_l
     beta=(beta_h-beta_l)*param_dum(2)+beta_l !18jan15
     phi1=(phi1_h-phi1_l)*param_dum(3)+phi1_l
     pinu1=(pinu1_h-pinu1_l)*param_dum(4)+pinu1_l

    open(299, file='adjusted_param.dat')
    write(299,*) Psimin, beta, pinu1, phi1
    !write(299,*) phi1,  gamma_psi, Psimin, beta, Rbar(1), sdeta
    close(299)
    open(299, file='adjusted_param.dat',status='old')
    read(299,*) Psimin, beta, pinu1, phi1
    close(299)

     pinu(1,1)=pinu1
     pinu(2,1)=pinu1
     pinu(1,2)=one-pinu1
     pinu(2,2)=one-pinu1

        call initstuff
        call stat_distr
        call prices_iter
        call simul_paths

        v_err(1)=   (AtoY-AtoY_dat)/AtoY_dat
        v_err(2)=   (spr_stdv-sprstdv_dat)/sprstdv_dat
        v_err(3)=   (debt_stdv-debtstdv_dat)/debtstdv_dat
        v_err(4)=   (d_stdv-dstdv_dat)/dstdv_dat
        v_err(5)=   (freqdef-freqdef_dat)/freqdef_dat
        v_err(6)=   (arrears-arrears_dat)/arrears_dat
        v_err(7)=   (debtserv-debtserv_dat)/debtserv_dat
        v_err(8)=   (debtserv_sd-debtserv_sd_dat)/debtserv_sd_dat
        v_err(9)=   (hhaircut-hhaircut_dat)/hhaircut_dat


        v_err(1)=   calibw_AtoY*v_err(1)
        v_err(2)=   calibw_sprsd*v_err(2)
        v_err(9)=   calibw_hairc*v_err(9)
        v_err(5)=   calibw_freq*v_err(5)
        v_err(6)=   calibw_def*v_err(6)
        v_err(4)=   calibw_dsd*v_err(4)

end if


if(calibrcase.eq.8) then
     beta=(beta_h-beta_l)*param_dum(1)+beta_l !18jan15
     phi1=(phi1_h-phi1_l)*param_dum(2)+phi1_l
     pinu1=(pinu1_h-pinu1_l)*param_dum(3)+pinu1_l

    open(299, file='adjusted_param.dat')
    write(299,*) beta, pinu1, phi1
    close(299)
    open(299, file='adjusted_param.dat',status='old')
    read(299,*) beta, pinu1, phi1
    close(299)

     pinu(1,1)=pinu1
     pinu(2,1)=pinu1
     pinu(1,2)=one-pinu1
     pinu(2,2)=one-pinu1

        call initstuff
        call stat_distr
        call prices_iter
        call simul_paths

        v_err(1)=   (AtoY-AtoY_dat)/AtoY_dat
        v_err(2)=   (spr_stdv-sprstdv_dat)/sprstdv_dat
        v_err(3)=   (debt_stdv-debtstdv_dat)/debtstdv_dat
        v_err(4)=   (d_stdv-dstdv_dat)/dstdv_dat
        v_err(5)=   (freqdef-freqdef_dat)/freqdef_dat
        v_err(6)=   (arrears-arrears_dat)/arrears_dat
        v_err(7)=   (debtserv-debtserv_dat)/debtserv_dat
        v_err(8)=   (debtserv_sd-debtserv_sd_dat)/debtserv_sd_dat
        v_err(9)=   (hhaircut-hhaircut_dat)/hhaircut_dat


        v_err(1)=   calibw_AtoY*v_err(1)
        v_err(2)=   calibw_sprsd*v_err(2)
        v_err(9)=   calibw_hairc*v_err(9)
        v_err(5)=   calibw_freq*v_err(5)
        v_err(6)=   calibw_def*v_err(6)
        v_err(4)=   calibw_dsd*v_err(4)

end if


if(calibrcase.eq.9) then
     Psimin=(Psimin_h-Psimin_l)*param_dum(1)+Psimin_l
     beta=(beta_h-beta_l)*param_dum(2)+beta_l !18jan15
     phi1=(phi1_h-phi1_l)*param_dum(3)+phi1_l
     pinu1=(pinu1_h-pinu1_l)*param_dum(4)+pinu1_l

     sdeta=(sdeta_h-sdeta_l)*param_dum(5)+sdeta_l
     rhoz=(rhoz_h-rhoz_l)*param_dum(6)+rhoz_l

    open(299, file='adjusted_param.dat')
    write(299,*) Psimin, beta, pinu1, phi1, sdeta, rhoz
    close(299)
    open(299, file='adjusted_param.dat',status='old')
    read(299,*) Psimin, beta, pinu1, phi1, sdeta, rhoz
    close(299)

     call tauchen(zero, sdeta, rhoz, 3.0_prec, nz, zj, piz)
     zj(1:nz)=exp(zj(1:nz))/two


     pinu(1,1)=pinu1
     pinu(2,1)=pinu1
     pinu(1,2)=one-pinu1
     pinu(2,2)=one-pinu1

        call initstuff
        call stat_distr
        call prices_iter
        call simul_paths

        v_err(1)=   (AtoY-AtoY_dat)/AtoY_dat
        v_err(2)=   (spr_stdv-sprstdv_dat)/sprstdv_dat
        v_err(3)=   (debt_stdv-debtstdv_dat)/debtstdv_dat
        v_err(4)=   (d_stdv-dstdv_dat)/dstdv_dat
        v_err(5)=   (freqdef-freqdef_dat)/freqdef_dat
        v_err(6)=   (arrears-arrears_dat)/arrears_dat
        v_err(7)=   (debtserv-debtserv_dat)/debtserv_dat
        v_err(8)=   (debtserv_sd-debtserv_sd_dat)/debtserv_sd_dat
        v_err(9)=   (sqrt(logy_var)-logy_sd_dat)/logy_sd_dat
        v_err(10)=   (logy_corr-logy_corr_dat)/logy_corr_dat
        !v_err(9)=   (hhaircut-hhaircut_dat)/hhaircut_dat


        v_err(1)=   calibw_AtoY*v_err(1)
        v_err(2)=   calibw_sprsd*v_err(2)
        !v_err(9)=   calibw_hairc*v_err(9)
        v_err(5)=   calibw_freq*v_err(5)
        v_err(6)=   calibw_def*v_err(6)
        v_err(4)=   calibw_dsd*v_err(4)

end if

if(calibrcase.eq.10) then
     beta=(beta_h-beta_l)*param_dum(1)+beta_l !18jan15
     phi1=(phi1_h-phi1_l)*param_dum(2)+phi1_l
     pinu1=(pinu1_h-pinu1_l)*param_dum(3)+pinu1_l

     sdeta=(sdeta_h-sdeta_l)*param_dum(4)+sdeta_l
     rhoz=(rhoz_h-rhoz_l)*param_dum(5)+rhoz_l


    open(299, file='adjusted_param.dat')
    write(299,*) beta, pinu1, phi1, sdeta, rhoz
    close(299)
    open(299, file='adjusted_param.dat',status='old')
    read(299,*) beta, pinu1, phi1, sdeta, rhoz
    close(299)

     call tauchen(zero, sdeta, rhoz, 3.0_prec, nz, zj, piz)
     zj(1:nz)=exp(zj(1:nz))/two


     pinu(1,1)=pinu1
     pinu(2,1)=pinu1
     pinu(1,2)=one-pinu1
     pinu(2,2)=one-pinu1

        call initstuff
        call stat_distr
        call prices_iter
        call simul_paths

        v_err(1)=   (AtoY-AtoY_dat)/AtoY_dat
        v_err(2)=   (spr_stdv-sprstdv_dat)/sprstdv_dat
        v_err(3)=   (debt_stdv-debtstdv_dat)/debtstdv_dat
        v_err(4)=   (d_stdv-dstdv_dat)/dstdv_dat
        v_err(5)=   (freqdef-freqdef_dat)/freqdef_dat
        v_err(6)=   (arrears-arrears_dat)/arrears_dat
        v_err(7)=   (debtserv-debtserv_dat)/debtserv_dat
        v_err(8)=   (debtserv_sd-debtserv_sd_dat)/debtserv_sd_dat
        v_err(9)=   (sqrt(logy_var)-logy_sd_dat)/logy_sd_dat
        v_err(10)=   (logy_corr-logy_corr_dat)/logy_corr_dat
        !v_err(9)=   (hhaircut-hhaircut_dat)/hhaircut_dat


        v_err(1)=   calibw_AtoY*v_err(1)
        v_err(2)=   calibw_sprsd*v_err(2)
        !v_err(9)=   calibw_hairc*v_err(9)
        v_err(5)=   calibw_freq*v_err(5)
        v_err(6)=   calibw_def*v_err(6)
        v_err(4)=   calibw_dsd*v_err(4)

end if

    val=sqrt(sum(v_err(:)**2._prec))

if((calibrcase.eq.9).or.(calibrcase.eq.10)) then

       write(44,'(11f9.4,3f9.6,f9.5,f9.5,f9.5,f7.3,f7.3,f7.3,f7.3,f7.3)')  rhoz, sdeta, pinu1, Psimin, phi1,  beta,   &
        & val, &
        & AtoY, Spr, freqdef, arrears, &
        & d_stdv, spr_stdv, &
        & debt_stdv, &
        & corrSpr, sdC, hhaircut, &
        & debtserv, debtserv_sd,logy_corr,sqrt(logy_var),length_aver_stat
else
       write(44,'(9f9.4,3f9.6,f9.5,f9.5,f9.5,f7.3,f7.3,f7.3)')  pinu1, Psimin, phi1,  beta,   &
        & val, &
        & AtoY, Spr, freqdef, arrears, &
        & d_stdv, spr_stdv, &
        & debt_stdv, &
        & corrSpr, sdC, hhaircut, &
        & debtserv, debtserv_sd, length_aver_stat !12oct21
end if

    step_calib=step_calib+1

 end subroutine calfun
